package com.cater.attendance.controller;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.cater.annotation.SystemControllerLog;
import com.cater.attendance.entity.AttendanceEvection;
import com.cater.attendance.service.AttendanceEvectionServiceImpl;
import com.cater.common.PageResult;
import com.cater.query.CommonQuery;

/**
* 描述：考勤管理_外出登记控制层
* @author yong
* @date 2018/06/11
*/
@Controller
@RequestMapping("/attendanceEvection")
public class AttendanceEvectionController {

	@Autowired
	private AttendanceEvectionServiceImpl service;

	/**
	 * @考勤管理_外出登记管理列表视图
	 * @return
	 */
	@RequestMapping("/list")
	public String list() {
		return "attendance/attendanceEvection/list";
	}

	/**
	 * @考勤管理_外出登记管理数据分页
	 * @param query
	 * @param request
	 * @return
	 */
	@RequestMapping("listData")
	@ResponseBody
	@SystemControllerLog(description = "查询考勤管理_外出登记分页数据")
	public PageResult<AttendanceEvection> listData(CommonQuery query, HttpServletRequest request) {
		PageResult<AttendanceEvection> result = service.findPageResult(query);
		return result;
	}

	/**
	 * 添加视图
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "addView")
	@RequiresPermissions("attendanceEvection:add")
	public String addView() {
		return "attendance/attendanceEvection/add";
	}

	/**
	 * 添加操作
	 * 
	 * @param t
	 * @return
	 */
	@RequestMapping(value = "add")
	@RequiresPermissions("attendanceEvection:add")
	@ResponseBody
	@SystemControllerLog(description = "添加考勤管理_外出登记")
	public JSONObject add(AttendanceEvection t, CommonQuery query) {
		JSONObject json = new JSONObject();
			String id = service.saves(t);
			if (StringUtils.isNotBlank(id)) {
				json.put("code", 200);
				json.put("message", "success");
			} else {
				json.put("code", 999);
				json.put("message", "fail");
			}
		return json;
	}

	/**
	 * 修改视图
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "editView")
	@RequiresPermissions("attendanceEvection:edit")
	public String editView(String id, Model model, CommonQuery query) {
		AttendanceEvection t = service.getById(id);
		model.addAttribute("attendanceEvection", t);
		return "attendance/attendanceEvection/update";
	}

	/**
	 * 修改操作
	 * 
	 * @param t
	 * @return
	 */
	@RequestMapping(value = "update")
	@RequiresPermissions("attendanceEvection:edit")
	@ResponseBody
	@SystemControllerLog(description = "修改考勤管理_外出登记")
	public JSONObject update(AttendanceEvection t, CommonQuery query) {
		JSONObject json = new JSONObject();
		try {
			service.update(t);
			json.put("code", 200);
			json.put("message", "修改成功");
		} catch (Exception e) {
			e.printStackTrace();
			json.put("code", 999);
			json.put("message", "修改失败");
		}
		return json;
	}

	/**
	 * 删除操作
	 * 
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/delete")
	@RequiresPermissions("attendanceEvection:delete")
	@SystemControllerLog(description = "删除考勤管理_外出登记")
	public JSONObject delete(String id) {
		JSONObject json = new JSONObject();
		try {
			service.delete(id);
			json.put("code", 200);
			json.put("message", "删除成功");
		} catch (Exception e) {
			e.printStackTrace();
			json.put("code", 999);
			json.put("message", "删除失败");
		}
		return json;
	}

}